package com.ruoyi.labdata.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.labdata.mapper.LabFlowDefinationMapper;
import com.ruoyi.labdata.mapper.LabFlowNodeMapper;
import com.ruoyi.labdata.domain.LabFlowDefination;
import com.ruoyi.labdata.domain.LabFlowNode;
import com.ruoyi.labdata.service.ILabFlowDefinationService;

/**
 * 流程定义Service业务层处理
 * 
 * @author winner pan
 * @date 2022-03-06
 */
@Service
public class LabFlowDefinationServiceImpl implements ILabFlowDefinationService {
	private static final String END = "End";
	private static final String START = "Start";
	@Autowired
	private LabFlowDefinationMapper labFlowDefinationMapper;
	@Autowired
	private LabFlowNodeMapper labFlowNodeMapper;

	/**
	 * 查询流程定义
	 * 
	 * @param workflowId
	 *            流程定义ID
	 * @return 流程定义
	 */
	@Override
	public LabFlowDefination selectLabFlowDefinationById(Long workflowId) {
		return labFlowDefinationMapper.selectLabFlowDefinationById(workflowId);
	}

	/**
	 * 查询流程定义列表
	 * 
	 * @param labFlowDefination
	 *            流程定义
	 * @return 流程定义
	 */
	@Override
	public List<LabFlowDefination> selectLabFlowDefinationList(LabFlowDefination labFlowDefination) {
		return labFlowDefinationMapper.selectLabFlowDefinationList(labFlowDefination);
	}

	/**
	 * 新增流程定义
	 * 
	 * @param labFlowDefination
	 *            流程定义
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertLabFlowDefination(LabFlowDefination labFlowDefination) {
		labFlowDefination.setCreateTime(DateUtils.getNowDate());
		int key = labFlowDefinationMapper.insertLabFlowDefination(labFlowDefination);
		generalNode(START, 1, labFlowDefination.getWorkflowId());
		generalNode(END, 2, labFlowDefination.getWorkflowId());
		return key;
	}

	private void generalNode(String nodeName, int nodeNo, Long workflowId) {
		LabFlowNode flowNode = new LabFlowNode();
		flowNode.setNodeName(nodeName);
		flowNode.setNodeNo(nodeNo);
		flowNode.setNodeTypeOpt("1");
		flowNode.setApprovalTypeOpt("1");
		flowNode.setStatusOpt("1");
		flowNode.setWorkflowId(workflowId);
		this.labFlowNodeMapper.insertLabFlowNode(flowNode);
	}

	/**
	 * 修改流程定义
	 * 
	 * @param labFlowDefination
	 *            流程定义
	 * @return 结果
	 */
	@Override
	public int updateLabFlowDefination(LabFlowDefination labFlowDefination) {
		return labFlowDefinationMapper.updateLabFlowDefination(labFlowDefination);
	}

	/**
	 * 批量删除流程定义
	 * 
	 * @param workflowIds
	 *            需要删除的流程定义ID
	 * @return 结果
	 */
	/*@Override
	public int deleteLabFlowDefinationByIds(Long[] workflowIds) {
		return labFlowDefinationMapper.deleteLabFlowDefinationByIds(workflowIds);
	}*/

	/**
	 * 删除流程定义信息
	 * 
	 * @param workflowId
	 *            流程定义ID
	 * @return 结果
	 */
	@Override
	public int deleteLabFlowDefinationById(Long workflowId) {
		if(checkAllowDelete(workflowId)==null){
			return labFlowDefinationMapper.deleteLabFlowDefinationById(workflowId);
		}else{
			LabFlowDefination labFlowDefination = this.selectLabFlowDefinationById(workflowId);
			labFlowDefination.setStatusOpt("3");
			return labFlowDefinationMapper.updateLabFlowDefination(labFlowDefination);
		}
	}

	public List<LabFlowDefination> selectAllFlowDefination(LabFlowDefination labFlowDefination) {
		return labFlowDefinationMapper.selectAllFlowDefination(labFlowDefination);
	}

	public LabFlowDefination previewFlowDefination(Long workflowId) {
		if(workflowId==null){
			return null;
		}
		LabFlowDefination flowDefination = labFlowDefinationMapper.getFlowDefination(workflowId);
		if(flowDefination==null){
			throw new RuntimeException("错误，流程定义不存在或已被删除.流程定义Id:"+workflowId);
		}
		List<LabFlowNode> labFlowNodeList = labFlowNodeMapper.getAllFlowNodeByWorkflowId(workflowId);
		flowDefination.setNodeList(labFlowNodeList);
		return flowDefination;
	}
	public LabFlowDefination businessFlowDefination(String businessType) {
		if(businessType==null){
			return null;
		}
		LabFlowDefination queryFlowDefination = new LabFlowDefination();
		queryFlowDefination.setBusinessType(businessType);
		List<LabFlowDefination> flowDefinationList = labFlowDefinationMapper.selectAllFlowDefination(queryFlowDefination);
		if(flowDefinationList.size()==0){
			throw new RuntimeException("错误，流程定义不存在或已被删除.流程代码:"+businessType);
		}
		queryFlowDefination = flowDefinationList.get(0);
		List<LabFlowNode> labFlowNodeList = labFlowNodeMapper.getAllFlowNodeByWorkflowId(queryFlowDefination.getWorkflowId());
		queryFlowDefination.setNodeList(labFlowNodeList);
		return queryFlowDefination;
	}
	
	public Integer checkAllowDelete(Long workflowId){
		return labFlowDefinationMapper.checkAllowDelete(workflowId);
	}

}
